'''
Created on Mar 24, 2010

@author: jhlee
'''
from OpenGL.GL import *
from OpenGL.GLU import *
from PIL import Image

class BitmapMaterial:
    def __init__(self):
        self.w = None
        self.h = None
        
    def load(self, fn):
        im = Image.open(fn)
        try:
            ix, iy, image = im.size[0], im.size[1], im.tostring("raw", "RGBA", 0, -1)
        except SystemError:
            ix, iy, image = im.size[0], im.size[1], im.tostring("raw", "RGBX", 0, -1)
        
        ID = glGenTextures(1)
        
        # make it current one
        glBindTexture(GL_TEXTURE_2D, ID)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glPixelStorei(GL_UNPACK_ALIGNMENT,1)

        glTexImage2D(GL_TEXTURE_2D, 0, 3, ix,iy, 0, 
                     GL_RGBA, GL_UNSIGNED_BYTE, image)
        print 'ErrorCode' + str(glGetError())

        self.w = ix
        self.h = iy
        self.textureId = ID
        
    def setupForRendering(self):        
        glEnable(GL_TEXTURE_2D)
        glBindTexture(GL_TEXTURE_2D, self.textureId)
    
if __name__ == "__main__":
    bm = BitmapMaterial()
    bm.load('twitter.png')
    